package cn.jeefast.controller;


import cn.jeefast.entity.Huankuan;
import cn.jeefast.service.HuankuanService;
import cn.jeefast.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaodongyun
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/huankuan")
public class HuankuanController {
    @Resource
    HuankuanService huankuanService;

    //保存到数据库
    @RequestMapping("/inserthuankuan")
    public String impPriceRecord() throws Exception {
        Huankuan huankuan = new Huankuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String num;
        String path = "";
        Integer integer;
        path = "C:\\Users\\Wade\\Desktop\\job\\清欠数据表格规范.xlsx";
        List<List<String>> returnList = ExcelUtil.readExcelWithSheet(new File(path), 0);
        integer = returnList.size();
        System.out.println(returnList.size());
        for (int i = 1; i < returnList.size(); i++) {
            if (returnList.get(i).get(0) == null) {
                continue;
            }
            huankuan.setName(returnList.get(i).get(0));
            huankuan.setNamenumber(returnList.get(i).get(1));
            huankuan.setBook(returnList.get(i).get(2));
            huankuan.setPhone(returnList.get(i).get(3));
            huankuan.setCarnumber(returnList.get(i).get(4));
            huankuan.setDaikuantype(returnList.get(i).get(5));
            huankuan.setDaikuanmoney(Double.valueOf(returnList.get(i).get(6)));
            if (returnList.get(i).get(7).equals("")) {

            }  else if (returnList.get(i).get(7).contains(".")) {
                String date[]=returnList.get(i).get(7).split("\\.");
                String jieben="";
                for(int j=0;j<date.length;j++){
                    if(j+1==date.length){
                        jieben+=date[j];
                    }else{
                        jieben+=date[j]+"-";
                    }
                }
                System.out.println(jieben);
                huankuan.setDaikuanbegindate(sdf.parse(jieben));
            }else {
                huankuan.setDaikuanbegindate(sdf.parse(returnList.get(i).get(7)));
            }
            if (returnList.get(i).get(8).equals("")) {

            } else {
                huankuan.setLilv(returnList.get(i).get(8));
            }

            if (returnList.get(i).get(9).equals("")) {

            } else {
                huankuan.setYuegong(Double.valueOf(returnList.get(i).get(9)));
            }
            if (returnList.get(i).get(10).equals("")) {

            } else if (returnList.get(i).get(10).contains(".")) {
                String date[]=returnList.get(i).get(10).split("\\.");
                String jieben="";
                for(int j=0;j<date.length;j++){
                    if(j+1==date.length){
                        jieben+=date[j];
                    }else{
                        jieben+=date[j]+"-";
                    }

                }
                System.out.println(jieben);
                huankuan.setJiebendate(sdf.parse(jieben));
            } else {
                huankuan.setJiebendate(sdf.parse(returnList.get(i).get(10)));
            }
            if (returnList.get(i).get(11).equals("")) {
                if(huankuan.getJiebendate()!=null){
                    huankuan.setJiexidate(huankuan.getJiebendate());
                }
            } else if (returnList.get(i).get(11).contains(".")) {
                String date[]=returnList.get(i).get(11).split("\\.");
                String jieben="";
                for(int j=0;j<date.length;j++){
                    if(j+1==date.length){
                        jieben+=date[j];
                    }else{
                        jieben+=date[j]+"-";
                    }
                }
                System.out.println(jieben);
                huankuan.setJiexidate(sdf.parse(jieben));
            } else {
                huankuan.setJiexidate(sdf.parse(returnList.get(i).get(11)));
            }
            if (returnList.get(i).get(12).equals("")) {

            } else {
                huankuan.setShoufu(Double.valueOf(returnList.get(i).get(12)));
            }
            if (returnList.get(i).get(13).equals("")) {

            } else {
                huankuan.setYue(Double.valueOf(returnList.get(i).get(13)));
            }
            if (returnList.get(i).get(14).equals("")) {

            } else {
                huankuan.setBeizhu(returnList.get(i).get(14));
            }
            if (returnList.get(i).get(15).equals("")) {

            } else {
                huankuan.setDanbao(returnList.get(i).get(15));
            }
            if (returnList.get(i).get(16).contains("中天")) {
                huankuan.setDepartmentname("中天");
                huankuan.setDepartmentid("2");
            } else if (returnList.get(i).get(16).contains("鑫安")) {
                huankuan.setDepartmentname("鑫安");
                huankuan.setDepartmentid("3");
            } else if (returnList.get(i).get(16).contains("汇邦")) {
                huankuan.setDepartmentname("汇邦");
                huankuan.setDepartmentid("4");
            } else if (returnList.get(i).get(16).contains("汇安")) {
                huankuan.setDepartmentname("汇安");
                huankuan.setDepartmentid("5");
            }
            huankuan.setCaozuoren("系统导入");
            huankuan.setHuankuantype(0);
            huankuan.setType(0);
            if (returnList.get(i).get(10).equals("")) {//没有还款时间   开始时间+30天
                huankuan.setDaikuanenddate(huankuanService.yujidate(huankuan.getDaikuanbegindate(), 30));
            } else {
                huankuan.setDaikuanenddate(huankuanService.yujidate(huankuan.getJiebendate(), 30));
            }
            System.out.println(i + 1);
            huankuanService.insertHuanKuan(huankuan);
        }


//  aaaService.save(aa);
        return "保存数据库成功";
    }

    @RequestMapping("/update")
    public String update() throws  Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Huankuan>list=huankuanService.selectList(null);
        for(int i=0;i<list.size();i++){
           String sd= sdf.format(list.get(i).getJiexidate());
            if(sd.equals("2019-02-28")){
                if(!list.get(i).getJiebendate().equals("")){

                }
            }
        }
        return  "更新";
    }
}

